window.onload = function() {
	seajs.use(['jquery-1.8.2.js', 'iscroll_module.js', 'mdialog_module.js', 'util_module.js'], function(Z, I, M, U) {
		U.fillHead();
		U.fillFooter();
		var userId = U.getParam('userId');
		var type = U.getParam('type');
		var curPage = 1;
		var pageSize = 4;
		var groupId = -1;
		var legalPerson = '';
		initPage();

		function initPage() { //初始化页面
			U.loadOpt(false);
			$('#content>div').show(200);
			$('.team_info').hide(200);
			$('.entreprise_info').hide();
			if(type == 1) {
				$('.entreprise_info').show(200);
				get_entreprise();
			}
			if(type == 2) {
				$('.team_info').show(200);
				get_teamInfo();
			}
			initEvent();
		}

		function initEvent() {
			U.loadOpt(false);
		}

		function get_entreprise() { //获得企业信息
			U.ajaxget('/entreprise/get/' + userId, {}, function(json) {
				var e = json.models.entrepriseVO;
				$('.e_name').text(e.name);
				$('.e_descpicl').text(e.legalPerson);
				$('.e_code').text(U.formPhone(e.cardCode));
				if(e.cannect == null || e.cannect == "null") {
					e.cannect = '未知';
				}
				$('.e_connect').text(e.cannect);
				$('.e_tel').text(U.formPhone(e.tel));
				$('.e_remark').text(e.remark);
				groupId = e.userGroupsVO.id;
				legalPerson = e.userGroupsVO.userId;
				get_memberList();
				get_poorList();
			});

		}

		function get_teamInfo() { //获取团队信息
			U.ajaxget("team/get/" + userId, {}, function(json) {
				if(json.success) {
					var u = json.models.userTeamVO;
					groupId = json.models.userTeamVO.id;
					legalPerson = u.userGroupsVO.userId;
					$('.t_name').text(u.userGroupsVO.name);
					var teamType = u.groupType == 1 ? '社会组织' : '志愿都协会';
					$('.type').text(teamType);
					$('.intro').text(u.intro);
					$('.hope_area').text(u.helpArea);
					$('.hope_type').text(u.requiremrnt);
					$('.remark').text(u.remark);
					get_memberList();
					get_poorList();
				}
			});

		}

		function get_memberList() { //帮扶队员
			$('tbody').html('');
			if(groupId == -1) {
				alert('暂未有团队,没有帮扶队员');
				return false;
			}
			var param = {
				curPage: 1,
				pageSize: 10,
				groupId: groupId
			};
			U.ajax('/groupDetail/queryBygroup', param, function(json) {
				if(json.success) {
					var result = json.models.groList;
					$(result).each(function(i,dom) {
						$('tbody').append('<tr id="tr'+i+'"><td class="id" data-id="'+this.userId+'" style="display: none">' + this.userId + '</td><td>' + this.name + '</td><td>' + this.position + '</td><td>' + U.formPhone(this.mobile) + '</td></tr>');
					});
					//跳转到结对帮扶
					$('tr').click(function(){
						var userid = $(this).find("td:first").text();
						location.href = "help_project.html?userId=" + userid;
					});
					//paging_handle(json.models.page, get_memberList);
				}
			});
		}

		function get_poorList() {
			$('.poor_list').html('');
			var param = {
				'curPage': curPage,
				'pageSize': pageSize
			};
			U.ajax('poverty/list/' + userId, param, function(json) {
				var result = json.models.povertyObjList;
				$(result).each(function() {
					$('.poor_list').append('<div id='+this.id+' class="poor_people"><div><span>' + this.name + '</span></div><div><span>住址：' + this.address + '</span></div><div><span>致贫原因：' + this.reason + '</span><span class="m_detail">详细信息</span></div></div>');
				});
				paging_handle(json.models.page, get_poorList);
				$('.poor_people').click(function() {
					window.open('poor_peoleDetail.html?id='+$(this).attr('id'));
				});
			});
		}

		function paging_handle(page, method) {
			//分页导航数据处理
			totalPage = page.totalPage;
			$("#paging .page").empty();
			var start_count = 0;
			try {
				if(curPage >= 10) {
					start_count = curPage - 5;
				}
			} catch(e) {}
			var lastsize = totalPage;
			lastsize = start_count + 10;

			if(lastsize >= totalPage) {
				lastsize = totalPage;
			}
			for(var i = start_count; i < lastsize; i++) {
				if(i + 1 == curPage) {
					$("#paging .page").append("<div style='color:white;border-color:white;background-color:#DC3F44'>" + (i + 1) + "</div>");
				} else {
					$("#paging .page").append("<div>" + (i + 1) + "</div>");
				}
			}
			$(".Record>span:nth-child(1)").text("共" + page.totalItem + "条记录");
			$(".Record>span:nth-child(2)").text(curPage + "/" + page.totalPage);
			$("#paging .goto_page>input").val(curPage);
			//分页导航数据处理完毕
			//处理单击事件
			$(".next_page").unbind("click").click(function() {
				if(!page.nextPage) {
					alert("已经是最后一页了!")
					return false;
				}
				curPage++;
				method();
			});
			$(".up_page").unbind("click").click(function() {
				if(curPage == 1) {
					alert("已经是第一页了!");
					return false;
				}
				curPage--;
				method();
			});
			$("#paging .page>div").unbind("click").click(function() {
				curPage = $(this).text();
				method();
			});
			$("#paging .goto").unbind("click").click(function() {
				try {
					var index = $("#paging .goto_page>input").val();
					if(index <= totalPage && index > 0 && !/^[+-]?[1-9]?[0-9]*\.[0-9]*$/.test(index)) {
						curPage = $("#paging .goto_page>input").val();
						method();
					} else {
						alert("请输入正确的页码！");
					}
				} catch(e) {
					alert("请输入正确的页码！");
				}
			});
			//单机事件绑定完毕
		}

	});
}